<?php

namespace common\models\backend;

use common\enums\StatusEnum;
use common\helpers\Url;
use Yii;

/**
 * This is the model class for table "{{%admin_bank}}".
 *
 * @property integer $id
 * @property string $bank_user_name
 * @property string $bank_name
 * @property string $bank_address
 * @property string $bank_account
 */
class AdminBank extends \yii\db\ActiveRecord
{
    public $name;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%admin_bank}}';
    }

    // public function scenarios()
    // {
    //     return [
    //         'bank' => ['bank_user_name', 'bank_name', 'bank_address', 'bank_account','state', 'pay_type'],
    //         'third_party' => ['state','bank_img', 'pay_type'],
    //     ];
    // }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [

            [['bank_user_name', 'bank_name', 'bank_account','bank_img','pay_type'], 'required'],
            [[ 'bank_user_name', 'bank_name'], 'string', 'max' => 50],
            [['bank_address'], 'string', 'max' => 100],
            [['state','pay_type','bz','top'],'safe']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'bank_user_name' => '开户名',
            'bank_name' => '银行名称',
            'bank_address' => '开户地址',
            'bank_account' => '银行账号',
            'pay_type' => '账户类型',
            'bank_img' => '二维码',
            'bz' => '收款人信息',
            'top' => '排序',
            'state' => '状态',
        ];
    }
    public static function getStates()
    {
        return [
            '使用中','禁用'
        ];
    }
    public static function getMap()
    {
        return [
            0=>'BANK',1=>'TRC20'
        ];
    }

    public static function getPayType($type)
    {
        $maps = self::getMap();
        return isset($maps[$type])? $maps[$type]:'未设置';
    }

    public static function getIcon()
    {
        return [
            0=>'@attachurl/pay_bank.svg',
            1=>'@attachurl/pay_wechat.svg',
            2=>'@attachurl/pay_alipay.svg'
        ];
    }
    public static function getState($state)
    {
        $states = static::getStates();
        return $states[$state];
    }
    
    public static function GetList()
    {
        $data = static::find()->where(['state'=>StatusEnum::ENABLED])->asArray()->groupBy('pay_type')->select(['pay_type'])->all();
        foreach ($data as $k => $v) {
            $data[$k]['name'] = static::getMap()[$v['pay_type']];
            $data[$k]['pay_type'] = $v['pay_type'];
            $data[$k]['icon'] = Url::to(static::getIcon()[$v['pay_type']]);
        }
        return $data;
    }

    public static function GetListByPayType($payType)
    {
        $list = static::find()->where(['state'=>StatusEnum::ENABLED, 'pay_type'=>$payType])->asArray()->orderBy('RAND()')->one();
        $res['name'] = static::getMap()[$payType];
        $res['list'] = $list;
        $res['icon'] = Url::to(static::getIcon()[$payType]);
        return $res;
    }
}
